package academic.ui.client.widgets.forms;

import ui.client.GenericContainerTag;
import ui.client.widgets.InputField;

import academic.ui.client.jquery.JQuery;
import academic.ui.client.rules.Rules;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.HasText;

public class ForgotPasswordForm extends FormWidget {

	
	

	private InputField email = new InputField("Email");
	
	private KeyDownHandler removeErrors = new KeyDownHandler() {

		@Override
		public void onKeyDown(KeyDownEvent event) {
			JQuery.fadeOutErrors();
		}
		
	};
	
	public ForgotPasswordForm() {
		super();
		div.setStyleName("forgotPassForm");
		div.add(email);
		div.add(new GenericContainerTag("span","Enter your email address and we'll email you a link to reset your password."));
		email.getField().setStyleName("longInput");
		div.add(this.errorMsgs);
		email.getField().addKeyDownHandler(removeErrors);
	}
	
	@Override
	public Boolean checkRules() {
		errorMsgs.clear();
		if("".equals(email.getText()))
		{
			email.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Email is required."));
		}
		if(!"".equals(email.getText()) && !Rules.validEmail(email.getText()))
		{
			email.addStyleName("invalid");
			errorMsgs.add(new GenericContainerTag("div","Email is not valid."));
		}
		return errorMsgs.getWidgetCount()<1; 
	}

	@Override
	public void removeErrors() {
		errorMsgs.clear();
		email.removeStyleName("missingValue");
		email.removeStyleName("invalid");
	}

	public HasText getEmail() {
		return email;
	}
}
